# -*- coding: utf-8 -*-
# @author： 范勇
# @datetime： 2021/4/3 13:40 
# @email： 2404066129@qq.com
import re
import pandas as pd
import os
XX = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
# 常用变量


def a1(p, k_name):
    xx = XX.copy()
    p = p.split(')')
    p.remove('')
    for i in p:
        dict_a = str_dict(i)
        if list(dict_a.keys())[0] == '周':
            print(dict_a['周'])
            for j in dict_a['周']:
                if '-' in j:
                    a = re.findall(r'\d+', j)
                    for g in range(int(a[0]), int(a[1]) + 1):
                        xx.remove(g)
                elif '-' not in j:
                    xx.remove(int(j))
        elif list(dict_a.keys())[0] == '双周':
            print(dict_a['双周'])
            for j in dict_a['双周']:
                if '-' in j:
                    a = re.findall(r'\d+', j)
                    for g in range(int(a[0]), int(a[1]) + 1):
                        if g % 2 == 0:
                            xx.remove(g)
                elif '-' not in j:
                    xx.remove(int(j))
        elif list(dict_a.keys())[0] == '单周':
            print(dict_a['单周'])
            for j in dict_a['单周']:
                if '-' in j:
                    a = re.findall(r'\d+', j)
                    for g in range(int(a[0]), int(a[1]) + 1):
                        if g % 2 == 1:
                            xx.remove(g)
                elif '-' not in j:
                    xx.remove(int(j))
    if len(xx) == 16:
        return k_name+'无课'+'\n'
    elif len(xx) == 0:
        return ''
    return k_name+str(xx)+'\n'
# 关键程序，判断哪个列表


def in_all(file_path, k_name):
    a = pd.read_excel(io=file_path, header=2, skipfooter=1)
    a.set_index('Unnamed: 0', inplace=True)
    for s in range(a.shape[0]):
        for n in range(a.shape[1]):
            new1 = ''
            myjk = a.iloc[s][n].split()
            for g in myjk:
                if '[周]' in g or '[双周]' in g or '[双周]' in g:
                    new1 = new1 + g + ')'
            print(new1)
            a.iloc[s][n] = a1(new1, k_name)
    return a
# 逻辑处理块处理每一天每一节课的数据，并转换成一个特殊的列表


def main():
    PD = pd.DataFrame()
    file_all_name = os.listdir('./临时/')
    for k in file_all_name:
        print(k, '文件正在执行')
        s = k
        k = k.replace('.xls', '')
        a = in_all('./临时/' + s, k)
        if PD.shape[0] == 0:
            PD = a
        else:
            PD = PD + a
    print(PD)
    PD.to_excel('给你看.xls')


def str_dict(p):
    d = {}
    if '双周' in p:
        p = p.replace('[双周]', '')
        d['双周'] = p.split(',')
    elif '单周' in p:
        p = p.replace('[单周]', '')
        d['单周'] = p.split(',')
    elif '周' in p:
        p = p.replace('[周]', '')
        d['周'] = p.split(',')
    return d


if __name__ == '__main__':
    main()
    # print(str_dict('1-6,12-16[周]'))
